package net.oschina.app.base;

import android.content.res.ColorStateList;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import net.oschina.app.R;
import net.oschina.app.factory.BaseFactory;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by wei03 on 2017/1/14.
 */

public abstract class BaseViewPagerFragment extends Fragment {
    @BindView(R.id.tabLayout)
    TabLayout mTabLayout;
    @BindView(R.id.viewPager)
    ViewPager mViewPager;
    private LinearLayout rootView;

    protected abstract BaseFactory getFragmentFactory();

    protected abstract String[] getPageTitile();

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        rootView = (LinearLayout) View.inflate(getContext(), R.layout.fragment_viewpager_base,null);
        ButterKnife.bind(this, rootView);
        initViewPager();
        return rootView;
    }


    private void initViewPager() {
        ColorStateList colorStateList = getContext().getResources().getColorStateList(R.color.tab_text_color_selector);
        mTabLayout.setTabTextColors(colorStateList);
        mTabLayout.setSelectedTabIndicatorColor(getResources().getColor(R.color.main_green));
        BasePagerAdapter basePagerAdapter = new BasePagerAdapter(getChildFragmentManager(), getPageTitile(),getFragmentFactory());
        mViewPager.setAdapter(basePagerAdapter);
        mTabLayout.setupWithViewPager(mViewPager);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }
}
